/*
 * =====================================================================================
 *
 *       Filename:  Http2ClientTest.cc
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  11/14/19 15:23:50
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  XXXXXXX (), XXXX@jd.com
 *        Company:  JDAI
 *        Copyright:  Copyright 2019, XXXXXXX
 *
 * =====================================================================================
 */

#include "Http2Client.h"

#include <iostream>

#include <uuid.h>
#include "cJSON.h"
#include<sstream>

#include "CommFunc.h"

using std::cout;
using std::endl;
using std::stringstream;


int gRecvIndex = 0;




static void parseRecvData(const void* data, int dataLen) {
    cJSON *node = NULL;
    cJSON *root = NULL;
    cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", recv:"   << (char*)data << endl;
    root = cJSON_Parse((const char*)data);
    if(root == NULL){
        cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", cJSON_Parse err:"    << endl;
        return ;
    }
        
    char* str = cJSON_Print(root);
    printf("str=%s\n", ((const char*)data));

    free(str);

    node = cJSON_GetObjectItem(root,"status");
    if(node==NULL){
        cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", no status: recv err:" <<   data <<  endl;
    }else {
        node = cJSON_GetObjectItem(root,"index");
        if(node == NULL){
            cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", no Index: recv err:" <<   data <<  endl;
        }else {
            gRecvIndex = node->valueint;
            cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", recv index:" <<   gRecvIndex <<  endl;

            node = cJSON_GetObjectItem(root,"audio");
            if(node == NULL){
                cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", no audio: recv err:" <<   data <<  endl;
            }else {
                string audioBase64 = "";
                string audioBin;    //PCM MP3 WAV


                audioBase64 = node->valuestring;
                cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", recv audio:" <<   audioBase64 <<  endl;
                base64_decode(audioBase64, &audioBin);

                if(audioBase64.size() != 0){
                    FILE *file = fopen("recv.bin","w");
                    cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", base64 :" <<   audioBase64 <<  endl;
                    //int ret = fwrite(audioBin.c_str, 1, audioBin.size, file);
                    fclose(file);
                    //cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", write len:" <<   ret <<  endl;
                }
            }
        }
    }

    cJSON_Delete(root);
}


class MyHttpListener: public HttpListener {
  public:
    void onRecvData(void* usrCtx, const void* data, int dataLen) {
        cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", recv len:" <<   dataLen <<  endl;
        //cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", recv len:" <<   dataLen << "data=" << (char*)data<<  endl;
        FILE *fp = fopen("./RecvAudio.json","a+");

        size_t ret = fwrite(data, 1, dataLen, fp);
        fclose(fp);
        cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", write len:" <<   ret <<  endl;
        //parseRecvData(data, dataLen);
    } 

    void onRecvData(void* usrCtx, string& data) {
        cout <<"F:" << __FILE__ <<  ". f:" << __func__ << ", recv len:" <<   data.size()<<  endl;

    }

    void onError(int errno) {
        cout <<"F:" << __FILE__ <<  ". f:" << __func__ << endl;
    }

};

string to_string(int n)
{
        stringstream ss;//创建一个字符串流对象ss内容为空
        ss<<n;//向流中输入内容n
        return ss.str();//调用str函数
}

int main(int argc, char* argv[]) {
    string msg = "北京";
    char buf[1024];
    //uuid_t uu;
    //uuid_generate( uu );
    //uuid_unparse_lower(uu, buf);
    

    //string uuid(buf);
    string uuid = "56a847e6-84c0-4c01-bf4b-d566f2d2dd11";

    int sendSeqID = -1;


    string seqIDStr = to_string(sendSeqID);
    string recvSeqID = "0";


    MyHttpListener* pMyHttpListener = new MyHttpListener();
    Http2Client* pHttp2Client = new Http2Client(pMyHttpListener);


    //pHttp2Client->setSrvURL("https://nghttp2.org/");
    pHttp2Client->setSrvURL("127.0.0.1:8802/HttpService/onHttpRequest");


    while(true){
        string headList[] = {
            "Service-Type:synthesis", 
            "Application-Id:123",
            "user-id:456",
            "Request-Id:" + uuid,
            "Sequence-Id:" + seqIDStr,
            "Protocol:v2.0",
            "Net-State:2",
            "Applicator:0",
            "Property:{\"platform\" : \"Linux\",\"version\" : \"0.0.0.1\",\"parameters\": {\"tte\": 1,\"tim\":0,\"vol\":2.0,\"sp\":1.0,\"sr\":16000,\"aue\":1}}"
        };
        pHttp2Client->addHead(headList, 9);
        pHttp2Client->post(msg);   //同步回调
        break;
        sleep(3);

    }

    delete pHttp2Client;

    pHttp2Client = NULL;

    delete pMyHttpListener;

    pMyHttpListener = NULL;

    cout << "f:" << __func__ << ", end" << endl;

    return 0;
}
